package com.parent.utils;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.parent.exceptions.CellPhoneNumberExcetion;
import com.parent.exceptions.ChineseNameException;
import com.parent.exceptions.EmailException;

import java.math.BigDecimal;

/**
 * @Author yan
 * @Date 2023/8/29 0029 10:19
 * @Description
 */
public class CommonUtils {
    private static String replacement = "*";
    /**
     * 无效数字
     */
    private static char invalidNumber = '0';

    /**
     * 手机号脱敏
     *
     * @param cellPhoneNumber
     * @return
     */
    public static String cellPhoneNumberDesensitization(String cellPhoneNumber) {
        if (!Validator.isMobile(cellPhoneNumber)) {
            throw new CellPhoneNumberExcetion();
        }
        return DesensitizedUtil.mobilePhone(cellPhoneNumber);
    }

    /**
     * 姓名脱敏
     *
     * @param name
     * @return
     */
    public static String chineseNameDesensitization(String name) {
        if (!Validator.isChineseName(name)) {
            throw new ChineseNameException();
        }
        return DesensitizedUtil.chineseName(name);
    }

    /**
     * 只对中文有效
     *  赵小来==》赵XL
     * @param name
     * @return
     */
    public static String chineseName(String name) {
        if (Validator.isChineseName(name)) {
            String chineseName = DesensitizedUtil.chineseName(name);
            //拿到姓氏
            String replace = chineseName.replace("*", "");
            int length = replace.length();
            //拿到名
            String replace1 = name.substring(length);
            //名转首字母
            String letter = PinyinUtil.getFirstLetter(replace1, "").toUpperCase();//nh

            StringBuffer stringBuffer = new StringBuffer();
            String toString = stringBuffer.append(replace).append(letter).toString();
            name = toString;
        }
        return name;
    }

    /**
     * 邮箱脱敏
     *
     * @param email
     * @return
     */
    public static String emailDesensitization(String email) {
        if (!Validator.isEmail(email)) {
            throw new EmailException();
        }
        return DesensitizedUtil.email(email);
    }


    /**
     * BigDecimal保留有效小数 BigDecimal==null 设置为0
     * @param decimal
     * @return
     */

    public static BigDecimal keepValidDecimals(BigDecimal decimal) {
        if (decimal == null) {
            decimal = BigDecimal.ZERO;
        }
        else {
            String decimalStr = decimal.toString();
            if (decimalStr.contains(".")) {
                String[] split = decimalStr.split("\\.");
                String a = split[1];
                int length = a.length();
                StringBuffer reverse = new StringBuffer(a).reverse();
                String string = reverse.toString();
                Integer indexEnd = null;
                for (int i = 0; i < length; i++) {
                    char c = string.charAt(i);
                    if (!ObjectUtil.equal(c, invalidNumber)) {
                        int index = length - i;
                        indexEnd = index;
                        break;
                    }
                }
                decimal = indexEnd == null ? decimal : decimal.setScale(indexEnd, BigDecimal.ROUND_HALF_UP);
            }
        }
        return decimal;
    }
}
